/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.momarketing.entity.MicroProductCategory;
import com.boxin.ims.modules.momarketing.dao.MicroProductCategoryDao;

/**
 * 微网站产分类Service
 * @author Jakemanse
 * @version 2013-11-25
 */
@Component
@Transactional(readOnly = true)
public class MicroProductCategoryService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(MicroProductCategoryService.class);
	
	@Autowired
	private MicroProductCategoryDao microProductCategoryDao;
	
	public MicroProductCategory get(Long id) {
		return microProductCategoryDao.findOne(id);
	}
	

	public List<MicroProductCategory> findCategoryListByProjectId(Long projectId){
		DetachedCriteria dc = microProductCategoryDao.createDetachedCriteria();
		dc.createAlias("project", "project");
		dc.add(Restrictions.like("project.id", projectId));
		dc.addOrder(Order.desc("sort"));
		return microProductCategoryDao.find( dc);
	}
	
	public Page<MicroProductCategory> find(Page<MicroProductCategory> page, MicroProductCategory microProductCategory) {
		DetachedCriteria dc = microProductCategoryDao.createDetachedCriteria();
		if (microProductCategory.getUser()!=null && microProductCategory.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", microProductCategory.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(microProductCategory.getName())){
			dc.add(Restrictions.like("name", "%"+microProductCategory.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return microProductCategoryDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(MicroProductCategory microProductCategory) {
		microProductCategoryDao.save(microProductCategory);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		microProductCategoryDao.deleteById(id);
	}
	
}
